package poker.preflopsim;

import java.util.ArrayList;
import java.util.List;

import poker.Card;
import poker.Deck;
import poker.HandPowerRating;
import poker.Human;
import poker.Player;
import poker.Printer;

public class GameRoundNoGUI
{
	private List<Player> players;
	private Deck deck;
	private int numberOfRounds;
	private ArrayList<Player> winners;
	
	private Card[] communityCards;
	private PlayerStats playerStats;
	private Card[] startCards;

	public PlayerStats getPlayerStats() { return playerStats; }
	
	public Card[] getCommunityCards() {
		return communityCards;
	}

	
	public GameRoundNoGUI(int numberOfPlayers, int numberOfRounds, Card[] startCards)
	{	
		this.numberOfRounds = numberOfRounds;
		this.startCards = startCards;
		communityCards = new Card[5];
		
		players = createPlayers(numberOfPlayers);
		players.get(0).setPocketCards(startCards[0]);
		players.get(0).setPocketCards(startCards[1]);
		
		playerStats = new PlayerStats();
	}
	
	private ArrayList<Player> createPlayers(int num)
	{
		ArrayList<Player> players = new ArrayList<Player>();
		
		for(int i = 0; i < num; i++)
		{
			Player p = new Human(100, Integer.toString(i));
			players.add(p);
		}
		
		return players;
	}
	
	public void start()
	{
		while(numberOfRounds > 0)
		{
			deck = new Deck(startCards);
			dealPocketCards();
			dealCommunityCards();		
			finishRound();			
		}
	}

	//vi trenger vel ikke å dele ut ett og ett kort her?
	public void dealPocketCards()
	{	
		for (int i = 1; i < (players.size()); i++)
		{
			players.get(i).setPocketCards(deck.getNextCard());
			players.get(i).setPocketCards(deck.getNextCard());
		}
	}

	public void dealCommunityCards()
	{
		Card card1 = deck.getNextCard();
        communityCards[0] = card1;
        Card card2 = deck.getNextCard();
        communityCards[1] = card2;
        Card card3 = deck.getNextCard();
        communityCards[2] = card3;
        Card card4 = deck.getNextCard();
        communityCards[3] = card4;
        Card card5 = deck.getNextCard();
        communityCards[4] = card5;
	}
        
    public Card[] getHand(Player player)
    {
       Card[] cards = new Card[7];
       
       cards[0] = player.getPocketCards()[0];
       cards[1] = player.getPocketCards()[1];
      
       cards[2] = communityCards[0];
       cards[3] = communityCards[1];
       cards[4] = communityCards[2];
       cards[5] = communityCards[3];
       cards[6] = communityCards[4];

       return cards;
    }
	
	public void finishRound() 
	{				
		winners = new ArrayList<Player>();
		
		
		for (Player player : players) {
			if(winners.isEmpty())
			{
				winners.add(player);
				continue;
			}
			
			int compare = HandPowerRating.compare(getHand(player), getHand(winners.get(0)));
			if(compare == 1) {
				winners.clear();
				winners.add(player);
			}
			else if(compare == 0)
				winners.add(player);
		}
		
		if(winners.contains(players.get(0)))
		{
			if(winners.size() > 1)
				playerStats.incDraws();
			else
				playerStats.incWins();
		}
		else
			playerStats.incLosses();
		
		numberOfRounds--;
	}
	
}
